package com.hzdracom.zhangnn.mainfestdemo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Main3Activity extends AppCompatActivity {

    private TextView tvSDCard;
    private TextView tvSDCard2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvSDCard = (TextView) findViewById(R.id.sdcard);
        tvSDCard2 = (TextView) findViewById(R.id.sdcard2);

        tvSDCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                checkSDCard();
                checkPermission();
            }
        });
        tvSDCard2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestPermission();
            }
        });
    }


    private void checkPermission() {
        if (!PermissionUtil.checkPermission(this, PermissionUtil.REQUEST_STORAGE)) {
            showPermission();
        } else {
            Toast.makeText(this, "已经有该权限", Toast.LENGTH_SHORT).show();
        }
    }

    private void showPermission() {
        if (PermissionUtil.shouldShowRequest(this, PermissionUtil.REQUEST_STORAGE)) {
            requestPermission();
        } else {
           //此时需要判断是第一次请求权限还是用户拒绝之后选择了不在提示
            PermissionUtil.showMissingPermissionDialog(this, new PermissionUtil.IPermissionListener() {
                @Override
                public void cancel() {
                    finish();
                }

                @Override
                public void set() {

                }
            });
        }
    }

    private void requestPermission() {
        PermissionUtil.requestPermissions(this, PermissionUtil.REQUEST_STORAGE_CODE, PermissionUtil.REQUEST_STORAGE);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PermissionUtil.REQUEST_STORAGE_CODE) {
            if (grantResults[0] == 0) {
                Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
            } else if (grantResults[0] == -1) {
//              Toast.makeText(this,"授权失败",Toast.LENGTH_SHORT).show();
                PermissionUtil.showMissingPermissionDialog(this, new PermissionUtil.IPermissionListener() {
                    @Override
                    public void cancel() {
                        Main3Activity.this.finish();
                    }

                    @Override
                    public void set() {
                        //
                    }
                });
            } else {
                Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    @Override
    protected void onResume() {
        super.onResume();
//        checkPermission();
    }
}
